package com.mrx.compose.test.ui

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.rememberWindowState
import com.mrx.compose.test.config.WindowConfig

abstract class BaseWindow {

    protected abstract val title: String

    protected abstract val size: DpSize

    protected open val scrollbar: Boolean = true

    @Composable
    fun mainWindow(onClose: () -> Unit) {
        val windowState = rememberWindowState().apply {
            size = size
            position = WindowPosition(Alignment.Center)
        }
        Window(
            onCloseRequest = onClose,
            title = title,
            icon = WindowConfig.image,
            resizable = false,
            state = windowState
        ) {
            Row(modifier = Modifier.maxSizeBorder()) {
                val stateVertical = rememberScrollState(0)
                Column(modifier = Modifier.weight(1F).maxWidthBroder().verticalScroll(stateVertical)) {
                    app(window)
                }
                if (scrollbar) {
                    VerticalScrollbar(
                        modifier = Modifier.fillMaxHeight(),
                        adapter = rememberScrollbarAdapter(stateVertical)
                    )
                }
            }

        }
    }

    @Composable
    protected abstract fun app(window: ComposeWindow)

    protected fun Modifier.border(color: Color = Color.Red) = this.border(BorderStroke(1.dp, color))

    protected fun Modifier.maxSizeBorder(color: Color = Color.Red) = this.fillMaxSize().border(color)

    protected fun Modifier.maxWidthBroder(color: Color = Color.Red) = this.fillMaxWidth().border(color)

}